﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
#if !WINDOWS_PHONE


#endif

namespace DARE
{
    [AttrTypeDependentSerializable(typeof(DARE.Save.DataStructures.CFloorModelData))]
    public class CFloorModel : CBasicModel
    {

    #region ctor

        public CFloorModel() : 
            base(50, 0, -50)
        {
            Initialize();
        }

        public CFloorModel(string name) : 
            base(name, 50, 0, -50)
        {
            Initialize();
        }

        private void Initialize()
        {
            //m_cullMode = CullMode.None;
            //CullMode = CullMode.None;
            Color = new Color(0.3f, 0.3f, 0.3f);
            SetUpVertices();
            CreateVertexBuffer();
            FillMode = FillMode.WireFrame;
        }

        public override AModel Clone()
        {
            CFloorModel clone = new CFloorModel(m_name);

            base.CopyPropertiesTo(clone);
            return clone;
        }

    #endregion

    #region methods

        private void SetUpVertices()
        {
            int length = (int)m_size.X;
            int depth = (int)m_size.Z;
            float height = m_size.Y;
            int index = 0;

#if WINDOWS_PHONE
            m_vertices = new VertexPositionNormalTexture[Math.Abs(length * depth) * 6];
#else
            m_vertices = new VertexPositionNormalTextureTangentBinormal[Math.Abs(length * depth) * 6];
#endif
            // points definition and normal
            Vector3 bottomLeft = Vector3.Zero;
            Vector3 bottomRight = Vector3.Zero;
            Vector3 topRight = Vector3.Zero;
            Vector3 topLeft = Vector3.Zero;
            Vector3 normal = Vector3.UnitY;
#if WINDOWS_PHONE
            // floor creation
            for (int x = 0; x < Math.Abs(length); ++x)
                for (int y = 0; y < Math.Abs(depth); ++y)
                {
                    bottomLeft = new Vector3(Math.Sign(length) * x, height, Math.Sign(depth) * y);
                    bottomRight = bottomLeft + Vector3.UnitX;
                    topLeft = bottomLeft - Vector3.UnitZ;
                    topRight = bottomRight - Vector3.UnitZ;

                    m_vertices[index++] = new VertexPositionNormalTexture(bottomLeft, normal, Vector2.Zero);
                    m_vertices[index++] = new VertexPositionNormalTexture(topLeft, normal, Vector2.Zero);
                    m_vertices[index++] = new VertexPositionNormalTexture(topRight, normal, Vector2.Zero);

                    m_vertices[index++] = new VertexPositionNormalTexture(bottomLeft, normal, Vector2.Zero);
                    m_vertices[index++] = new VertexPositionNormalTexture(topRight, normal, Vector2.Zero);
                    m_vertices[index++] = new VertexPositionNormalTexture(bottomRight, normal, Vector2.Zero);
                }
#else
            // floor creation
            for (int x = 0; x < Math.Abs(length); ++x)
                for (int y = 0; y < Math.Abs(depth); ++y)
                {
                    bottomLeft = new Vector3(Math.Sign(length) * x, height, Math.Sign(depth) * y);
                    bottomRight = bottomLeft + Vector3.UnitX;
                    topLeft = bottomLeft - Vector3.UnitZ;
                    topRight = bottomRight - Vector3.UnitZ;

                    m_vertices[index++] = new VertexPositionNormalTextureTangentBinormal(bottomLeft, normal, Vector2.Zero);
                    m_vertices[index++] = new VertexPositionNormalTextureTangentBinormal(topLeft, normal, Vector2.Zero);
                    m_vertices[index++] = new VertexPositionNormalTextureTangentBinormal(topRight, normal, Vector2.Zero);

                    m_vertices[index++] = new VertexPositionNormalTextureTangentBinormal(bottomLeft, normal, Vector2.Zero);
                    m_vertices[index++] = new VertexPositionNormalTextureTangentBinormal(topRight, normal, Vector2.Zero);
                    m_vertices[index++] = new VertexPositionNormalTextureTangentBinormal(bottomRight, normal, Vector2.Zero);
                }
#endif
        }

    #endregion

    }
}
